<?php

namespace app\models;

use app\common\utils\QyWechat;
use Yii;

/**
 * This is the model class for table "gky_qyh_user".
 *
 * @property integer $id
 * @property string $userid
 * @property string $name
 * @property string $position
 * @property string $mobile
 * @property string $gender
 * @property string $weixinid
 * @property string $avatar
 * @property integer $status
 * @property integer $qyh_id
 * @property string $graduate_school
 * @property string $xueli
 * @property string $xuewei
 * @property string $address
 * @property string $signature
 *
 * @property GkyQyhDeptUser[] $deptUsers
 * @property GkyQyh $qyh
 */
class GkyQyhUser extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_qyh_user';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userid'], 'required'],
            [['status','qyh_id'], 'integer'],
            [['userid', 'name', 'position', 'mobile', 'gender', 'weixinid', 'avatar', 'graduate_school', 'xueli', 'xuewei', 'address','signature'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [ 
            'id' => 'ID',
            'userid' => '成员编号',
            'name' => '姓名',
            'position' => 'Position',
            'mobile' => '手机',
            'gender' => '性别',
            'weixinid' => 'Weixinid',
            'avatar' => 'Avatar',
            'status' => 'Status',
            'qyh_id' => '企业号',
            'graduate_school' => '毕业院校',
            'xueli' => '学历',
            'xuewei' => '学位',
            'address' => '地址',
            'signature' => '签名图片'
        ];
    }
    
    public function getQyh(){
        return $this->hasOne(GkyQyh::className(),['id'=>'qyh_id']);
    }
    
    public function getDeptUsers(){

        return $this->hasMany(GkyQyhDeptUser::className(),['user_id'=>'userid']);
    }
    

    public static function findByUseridAndCorpid($userid, $coprid){
        $qyh = GkyQyh::findByCorpid($coprid);
        return GkyQyhUser::find()
            ->where(['userid'=>$userid,'qyh_id'=>$qyh->id])
            ->one();
    }

    /**
     * 根据id和corpid查询userid集合
     *
     * @param $ids
     * @param $corpid
     * @return array
     */
    public static function findUseridByList($ids, $corpid){

        $users = GkyQyhUser::find()->where(['id'=>$ids])->all();
        $result = [];
        foreach($users as $user){
            $result[] = $user->userid;
        }
        return $result;
    }

    /**
     * 从微信更新部门成员
     */
    public static function updataAll($corpid){

        $qyh = GkyQyh::findByCorpid($corpid);

        $options = array(
            'token'=>'gky',   //填写应用接口的Token
            'encodingaeskey'=>$qyh->aeskey,//填写加密用的EncodingAESKey
            'appid'=>$corpid,  //填写高级调用功能的appid
            'appsecret'=>$qyh->secret, //填写高级调用功能的密钥
        );

        $weObj = new QyWechat($options);

        $departments = GkyQyhDepartment::findByAll($corpid);
        foreach($departments as $department){
            $userListInfo = $weObj->getUserListInfo($department->dno,1);
            if($userListInfo !=false && $userListInfo['errcode'] == 0){
                foreach($userListInfo['userlist'] as $userInfo){
                    $user = GkyQyhUser::findByUseridAndCorpid($userInfo['userid'],$corpid);
                    if($user == null){
                        $user = new GkyQyhUser();
                        $user->userid = $userInfo['userid'];
                        $user->qyh_id = $qyh->id;
                    }
                    $user->name = isset($userInfo['name']) ? $userInfo['name'] : null;
                    $user->position = isset($userInfo['position']) ? $userInfo['position'] : null;
                    $user->mobile = isset($userInfo['mobile']) ? $userInfo['mobile'] : null;
                    $user->gender = isset($userInfo['gender']) ? $userInfo['gender'] : null;
                    $user->weixinid = isset($userInfo['weixinid']) ? $userInfo['weixinid'] : null;
                    $user->avatar = isset($userInfo['avatar']) ? $userInfo['avatar'] : null;
                    $user->status = isset($userInfo['status']) ? $userInfo['status'] : null;
                    $user->signature = isset($userInfo['signature']) ? $userInfo['signature'] : null;
                    foreach($userInfo['department'] as $deptId){
                        $deptUser = GkyQyhDeptUser::find()
                            ->where([
                                'dept_id'=>$qyh->id.$deptId,
                                'user_id'=>$userInfo['userid'],
                                'qyh_id' =>$qyh->id
                            ])
                            ->one();
                        if($deptUser == null){
                            $deptUser = new GkyQyhDeptUser();
                            $deptUser->dept_id = $qyh->id.$deptId;
                            $deptUser->user_id = $userInfo['userid'];
                            $deptUser->qyh_id = $qyh->id;
                            $deptUser->save();
                        }
                    }
                    $user->save();
                }
            }
        }
    }

    /**
     * 更新所有企业号的所有成员
     */
    public static function updataByAll(){
        $qyhs = GkyQyh::find()->all();
        foreach($qyhs as $qyh){
            GkyQyhUser::updataAll($qyh->corpid);
        }
    }
    

}
